package com.hdoit.jiayuntong.activity;

import com.hdoit.jiayuntong.R;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainFragmentActivity extends FragmentActivity {
     private FragmentTabHost mTabHost;
     private LayoutInflater layoutInflater;
     private Class<?> [] fragmentArray = {HomePageFragment.class,ShopCenterFragment.class,PublishFragment.class};
     private String [] mTextViewArray = {"首页","店铺中心","发布"};
     private int [] mImageViewArray = {R.drawable.homepage_tab_background,R.drawable.shopcenter_tab_background,R.drawable.publish_tab_background};
     
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_main_fragment);
    	initView();
    }

	private void initView() {
		layoutInflater = LayoutInflater.from(this);
		mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup(this,getSupportFragmentManager(),R.id.fragmentactivity);
		mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
		
		for(int i = 0; i < fragmentArray.length;i ++){
			TabSpec tabSpec = mTabHost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i));
		    mTabHost.addTab(tabSpec,fragmentArray[i],null);
		    mTabHost.setOnTabChangedListener(new OnTabChangeListener(){

				@Override
				public void onTabChanged(String tabId) {
					for(int i = 0; i < mTabHost.getTabWidget().getChildCount();i ++){
						if(mTabHost.getCurrentTab() == i){
							View v = mTabHost.getTabWidget().getChildAt(i);
							TextView tv = (TextView)v.findViewById(R.id.textview);
							ColorStateList csl = (ColorStateList)getBaseContext().getResources().getColorStateList(R.color.tab_item_text_orange);
							tv.setTextColor(csl);
						}else{
							View v = mTabHost.getTabWidget().getChildAt(i);
							TextView tv = (TextView)v.findViewById(R.id.textview);
							ColorStateList csl = (ColorStateList)getBaseContext().getResources().getColorStateList(R.color.tab_item_text_black);
							tv.setTextColor(csl);
						}
					}
				}
		    	
		    });
		}
	}
	
	private View getTabItemView(int index){
		View view = layoutInflater.inflate(R.layout.activity_tab_item_view, null);
		
		if (mImageViewArray.length != 0){
			ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
			imageView.setImageResource(mImageViewArray[index]);
		}
		
		TextView textView = (TextView)view.findViewById(R.id.textview);
		textView.setText(mTextViewArray[index]);
		
		if(index == 0){
			ColorStateList csl = (ColorStateList)getBaseContext().getResources().getColorStateList(R.color.tab_item_text_orange);
			textView.setTextColor(csl);
		}
		
		return view;
	}
}
